package com.zzx.test.thread.juc;

import org.junit.Test;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author zzx
 * 虚拟线程的创建方式
 * @since 2025/10/20 15:51
 */
public class VirtualThreadTest2 {

    int taskNum = 10;

    @Test
    public void test1() {
        try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
            for (int i = 0; i < taskNum; i++) {
                executor.execute(() -> System.out.println(1));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test2() {
        Thread.Builder.OfVirtual virtual = Thread.ofVirtual();
        virtual.start(() -> System.out.println(1));
        Thread thread = virtual.unstarted(() -> System.out.println(2));
        thread.start();
    }

    @Test
    public void test3() {
        ThreadFactory factory = Thread.ofVirtual().factory();
        factory.newThread(() -> System.out.println(1)).start();
    }

}
